import * as actionTypes from './actionTypes'
import {fromJS} from 'immutable'


const defaultState = fromJS({// 将js对象转化为immutable对象
    focused: false,
    mouseIn: false,
    list: [],
    page: 1,
    totalPage: 1,
});

// reducer处理state时候不能对原始的state修改，可是写着很容易失误对原始的state修改，那么久可以使用immutable.js生成一个immutable对象来解决该问题
export default (state=defaultState, action) => {
    switch(action.type){
        case actionTypes.SEARCH_Focus:
            return  state.set('focused', true);
        case actionTypes.SEARCH_Blur:
            return  state.set('focused', false);
        case actionTypes.CHANGE_LIST:
            // return  state.set('list', action.data).set('totalPage', action.totalPage);
            return state.merge({
                list: action.data,
                totalPage: action.totalPage
            })
        case actionTypes.MOUSE_ENTER:
            return  state.set('mouseIn', true);
        case actionTypes.MOUSE_LEAVE:
            return  state.set('mouseIn', false);
        case actionTypes.CHANGE_PAGE:
            return  state.set('page', action.page);
        default:
            return state;
    }
    // if(action.type === actionTypes.SEARCH_Focus) {
    //     return state.set('focused', true); // 这样写是因为引入了不可变对象immutable对象
    //     // return {
    //     //     focused: true
    //     // }
    // }
    // if(action.type === actionTypes.SEARCH_Blur) {
    //     // return {
    //     //     focused: false
    //     // }
    //     return state.set('focused', false);
    // }
    // if(action.type === actionTypes.CHANGE_LIST) {
    //     return state.set('list', action.data);
    // }
    // return state;
}